Repository navigation
feat(agents): 资源库指令只写标记块,多套指令各自开关并可限定 Agent - #261
Conversation
- 指令文件里 Core 只写 <!-- lithe:begin … --> 到 <!-- lithe:end --> 之间的一块,块外的用户文字逐字节保留;新块追加在用户文字之后,多余的块和落单的标记行清掉,指令为空时去掉整块,重复写入不变大。 - 读取状态改为 absent/noBlock/current/stale/edited/tooLarge/unreadable,并报告块外用户文字的行数 ownLines;块被用户改过时写入要 replace。 - 指令集每套有 on 和 agents(空为全部),按顺序拼接后接 Agent 的 extra;去掉 active,读到旧文件时迁移成“该套开、其余关”,拼出的文字逐字节一致。 - 新操作 instructions/import:把块外用户文字并入新建或已有的一套,先备份,文件只剩块。 - agentLibrary.save 保存后由 Core 同步所有跟随资源库的 Agent,逐个回答 written/unchanged/skipped/failed,不覆盖用户改过的块。 - agentProviders.restore 放回指令文件时关闭该 Agent 的跟随(unfollowed),避免下次保存撤销恢复。 - 清理残留文件失败时记 agent.library.cleanup_failed,只记目标类别和错误类别;library.rs、backups.rs 不再静默丢弃清理错误。 - 协议更新记录:16 个已合入的 PR 移进时间线并回填日期与合入提交。 Refs 1lck/Lithe-IDEA#2654
| Ok(json!({ "revision": digest(&bytes) })) | ||
| // Each agent is written under the writing lock of its own, so a | ||
| // change elsewhere never waits for every agent's file. | ||
| let instructions = self.sync_instructions(runtime, &config, &request.library, None); |
There was a problem hiding this comment.
🔴 Restore silently removes personal instructions
After a user adds notes outside the block, sync_instructions hides those edits from restore when it updates the block. Restore compares against the new whole-file digest, then restores the older baseline without asking. The added notes disappear from the agent’s file.
Learn more
A restore returns the file to its original baseline, not to the file just before the latest sync. Backup creation keeps the first baseline but updates the record to match the entire file after each write. write_block carries user text forward when it updates the block, so this new digest also covers user edits made since the baseline. Restore then accepts the file without discard confirmation because its digest matches. It saves the current file in backup history, but the notes are no longer in the file the agent reads.
Example: Start with My notes, enable following, and append Use the staging environment outside the block. Change a library set and save it. A normal restore now puts back only My notes without asking about the added line.
Recommended fix: Preserve evidence of edits outside the block across automatic rewrites. Use it to keep modifiedSince true and require discardLaterChanges when restoring the baseline would remove those edits. Add a save–edit–save–restore regression test.
Was this helpful? React with 👍 or 👎 to provide feedback.
| .iter_mut() | ||
| .find(|set| set.id == id) | ||
| .ok_or_else(unknown_item)?; | ||
| if !set.text.contains(own) { |
There was a problem hiding this comment.
🔴 Import drops distinct personal instructions
When a personal instruction matches only part of another sentence, import treats it as a duplicate. import_instructions then removes the original from the agent’s file without adding it to the library.
Learn more
Import moves text rather than copying it. import_instructions extracts the user's text, composes the updated library, and rewrites the site to contain only the managed block. A substring match does not establish that the library preserves the instruction as a standalone passage.
Example: Given the personal instruction Run tests. and a destination set containing Ignore this obsolete rule: Run tests., import removes the original line while leaving only the instruction to ignore it.
Recommended fix: Match complete passages with boundaries, such as the whole set or a complete paragraph-delimited passage, before deciding not to append. Test both a true duplicate and text embedded inside a different instruction.
Was this helpful? React with 👍 or 👎 to provide feedback.
| let bytes = serde_json::to_vec_pretty(&file).map_err(|_| write_failed())?; | ||
| write_replacing(&config.directory.join(LIBRARY_FILE), &bytes) | ||
| .map_err(|_| write_failed())?; |
There was a problem hiding this comment.
🔴 Import locks users out of the library
When an import pushes the library past 8 MiB, store_library commits a file load rejects. The import rewrites the source file and returns success, but users can no longer read or save the library.
Learn more
Library validation limits individual sets and per-agent text, not the total serialized size. import_instructions validates the new library and checks the generated instruction file, then replaces that file before saving the library. The new library can exceed 8 MiB even when the generated file is small, because most of the stored text need not apply to the importing agent. save also cannot read a library over that limit to check its revision.
Example: With a readable library just 8 KiB below the limit, import a 16 KiB instruction file into a new scoped set. The set and instruction file are both valid, but Core saves a library it cannot read back.
Recommended fix: Serialize the proposed library and enforce MAX_LIBRARY_FILE_BYTES before replacing the source instruction file. Reuse the validated bytes for the commit and apply the same size check to ordinary saves.
Was this helpful? React with 👍 or 👎 to provide feedback.
| let _ = write_replacing(&real, file.as_bytes()); | ||
| backups::undo(taken); |
There was a problem hiding this comment.
🔴 Failed import deletes its recovery copy
When saving the library and restoring the original file both fail, import_instructions still discards the new backup. undo deletes the pre-import copy even though the agent’s file still holds the replacement.
Learn more
replace_file records a backup before installing the import's replacement. If saving the library fails, the import attempts to put the previous instruction file back. If that write fails too, removing the backup discards the copy of the exact pre-import bytes even though the replacement is still present. In particular, an edited block the user agreed to replace is not included in the imported text.
Example: A storage error stops both the library commit and the write that would restore the original instruction file. After a first change with replace: true, the original edited block exists only in the new backup. The failure path deletes that backup and reports only the library write failure.
Recommended fix: Undo the backup only after the original file has been restored successfully. On rollback failure, preserve the recovery copy and record the distinct failure in the unified log with a stable error category and no file content.
Was this helpful? React with 👍 or 👎 to provide feedback.
关联
改动
都是 API-45、
agents能力 0 内的补充,不升版本。插件后端协议不变,四个 SDK 不需要改。新增错误码agentLibrary.setExists,新增日志事件agent.library.cleanup_failed。一、指令文件只写标记块
<!-- lithe:begin (written by Lithe from the agent library) -->到<!-- lithe:end -->之间的一块。块外的用户文字逐字节保留,只把紧挨块的空行收成一行。\r\n时块内也用\r\n。<!-- lithe:begin开头、以-->结尾就算数,用户改了中间的字也认;块从首行到其后第一个尾行。<!-- lithe:写成<!-- lithe :,不会提前结束块。fileTooLarge)。state.json改记块内文字的哈希。已有文件的迁移:以前整份写过、哈希一致的文件,下次写成只有块;其他没有块的文件都算用户文字,块追加在后面,不需要确认。agentLibrary.read的instructions.state改为七种:absent、noBlock、current、stale、edited、tooLarge、unreadable;新增ownLines(块外用户文字的行数,tooLarge、unreadable不带)。edited)时,写入要replace: true。agentLibrary.otherInstructions收窄为“块里有不是 Lithe 写的内容”。二、多套指令
on(read总会给出)和agents(最多 256 个不重复的 Agent key,空为全部);去掉active。sets顺序取开着且给该 Agent 的每一套,去掉尾部空白、跳过空的,再接该 Agent 的extra,段间空一行,末尾一个换行。active的已有library.json时转换成“那一套开、其余关”,拼出的文字逐字节一致(测试覆盖);active指向不存在的套报invalidLibraryFile。save带active报invalidLibrary。三、并入块外的用户文字
agentLibrary.apply新操作{category:"instructions", op:"import", id, name?, revision, replace?}。name:新建一套,开着,只给这个 Agent;id已被占用报新错误码agentLibrary.setExists。name:追加到已有那一套的末尾;已经包含同样文字就不重复加;这一套会打开,它有 Agent 列表时补上这个 Agent。enabled都变成 true。顺序:先备份文件,再把文件写成只剩块,然后保存资源库;资源库存不下就把文件放回。replace,这部分只留在备份里。四、保存后由 Core 同步
agentLibrary.save存下资源库后,按agents.list的顺序给每个跟随资源库的 Agent 写块,不覆盖用户改过的块。instructions,逐个 Agent 给出written、unchanged、skipped(原因otherInstructions、notGranted、noInstructions、instructionsOff)或failed(写入的拒绝码)。一个 Agent 失败不影响其他 Agent,资源库照样保存。不跟随的 Agent 不列出。五、恢复后停止跟随
agentProviders.restore放回指令文件时,先忘掉写入记录,再把该 Agent 的enabled关掉,回答新增unfollowed。否则下一次保存会把块写回去,等于撤销了恢复。library.json读不了时不改它,unfollowed为 false。日志agent.providers.restore_applied新增必填字段unfollowed。六、清理失败不再静默
agent.library.cleanup_failed(warn),字段target(temporary、staging、skillImport、backup、backupRecord)和error(只记 io 错误类别,不记路径或内容,残留文件可能含配置或密钥)。library.rs和library/backups.rs里原来用let _ =丢弃的 14 处清理错误,改为经remove_leftover处理:目标不存在算正常收尾,其他错误记这个事件。undo和恢复中途存回备份记录失败也记录,否则之后恢复失败时查不到原因。library/providers.rs插件修改失败后放回供应商列表那一处加log-exempt:插件那次失败已由agent.providers.change_failed记录。七、文档
shared/plugins/reference/domains/agents.md中英各加“指令文件”一节;rust/contracts/rust-core-api.md改 read、save、apply、restore 四行;API 总表 API-45 两行;能力版本表agents0 两行。对宿主的影响
桌面端在升级固定的 Core 提交前必须先改:这个 Core 拒绝带
active的保存,read每套带on,状态值换成七种,保存回答带逐 Agent 的同步结果。桌面接入在 1lck/Lithe-IDEA#2654 的后续工作包里做,届时升级PINNED_CORE_REVISION。按维护者的决定,不考虑旧版 Lithe 和旧 Core 的兼容与降级,只保留上面两项已有数据的迁移。已知限制
验证(macOS arm64 本机)
cargo test -p lithe-agent-runtime -p lithe-diagnosticscargo clippy -p lithe-agent-runtime -p lithe-diagnostics --all-targets --all-features -- -D warningsx86_64-pc-windows-msvc,同上两个 crate)cargo fmt --all --checknode shared/plugins/scripts/verify-docs.mjsnode scripts/logging/generate-log-events.mjs --checkscripts/rust-core/verify-rust-core-comments.sh agent-runtime diagnosticsnode scripts/rust-core/verify-platform-tests.mjs新增
rust/crates/agent-runtime/src/tests/library/instructions.rs(11 个测试)和共享样例rust/fixtures/agents/library-instructions-v1.json(12 个块布局用例和全部状态、结果取值,测试会读取它)。覆盖:块外文字不变;旧整份文件迁移;无块文件追加;块内被改后不覆盖、replace后覆盖;标记行失效;多块与残留结束行收拾;多套按顺序和 Agent 过滤拼接;active迁移逐字节一致;并入后文件只剩块且有备份;保存后逐 Agent 的同步结果;恢复后跟随关闭;清理失败只记一条不含路径的日志。测试没有 sleep,文件读写都在 Harness 的临时目录里。没有跑整个 workspace:日志目录改了,CI 会选全部 crate。